package sm.clientHandler;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;

import sm.server.*;
import sm.utils.*;



public class ServConnect extends Thread{
	
	private Socket Servsocket;
	private BufferedReader inFromServ;
	private DataOutputStream outToServ;
	
	public void run() {
		
		System.out.println("This is the ServConnect thread");
		System.out.println("Attempting to connect with the SM..");
		try {
			Servsocket = new Socket("10.12.0.128", PropertySet._SerMngSocket);
			inFromServ = new BufferedReader(new InputStreamReader(Servsocket.getInputStream()));
			outToServ = new DataOutputStream(Servsocket.getOutputStream());
			System.out.println("Connected to Server");
			Message request = new Message();
			request.setId("001");
			request.setHeader("request");
			request.setCommand(PropertySet._cmdGetGws);
			request.setData(" ");
			outToServ.writeBytes(request.toString());
			outToServ.flush();
			System.out.println("Message sent");
			
			Message response = new Message();
			String test = inFromServ.readLine();
			System.out.println(test);
			
			Message received = response.parse(test);
			String [] receivedData = received.getData().split(PropertySet._msgDataSplit);
			
			for (int i=0; i<receivedData.length;i++ ){
				System.out.println(receivedData[i]);
								
			}
			
			System.out.println("Message received");
			boolean check = received.checkValidity();
			System.out.println(check);
			
			
			
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
	}
}
